<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">{{ `共${total}条记录` }}</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="excelOut">导出</el-button>
          <el-button size="small" type="primary" @click="addUserApi">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card v-loading="loading">
        <el-table border :data="UserList">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formOfEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <template slot-scope="{ row }">{{ row.timeOfEntry | formatDate }}</template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState">
            <template slot-scope="scope">
              <el-switch
                :value="scope.row.enableState===1"
              >/>
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="280">
            <template slot-scope="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delUserApi(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="page.size"
            :current-page="page.page"
            @current-change="changeFn"
          />
        </el-row>
      </el-card>
      <addemployee
        :show-dialog.sync="showDialog"
        @update="getUserList"
      />
    </div>
  </div>
</template>

<script>
import { getUserAll, delUserApi } from '@/api/employees'
import employees from '@/api/constant/employees'
import addemployee from './component/add-employee'
import { formatDate } from '@/filters'

export default {
  components: {
    addemployee
  },
  data() {
    return {
      showDialog: false,
      loading: false,
      UserList: [],
      page: {
        page: 1,
        size: 10
      },
      total: null
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      this.loading = true
      const { rows, total } = await getUserAll(this.page)
      this.UserList = rows
      this.total = total
      this.loading = false
    },
    changeFn(page) {
      this.page.page = page
      this.getUserList()
    },
    formOfEmployment(row, column, cellValue, index) {
      const newObj = employees.hireType.filter(itm => itm.id === cellValue)
      return newObj[0] ? newObj[0].value : ''
    },
    async delUserApi(id) {
      try {
        await this.$confirm('确定删除嘛')
        await delUserApi(id)
        this.getUserList()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    addUserApi() {
      this.showDialog = true
    },
    async excelOut() {
      const headers = {
        '姓名': 'username',
        '手机号': 'mobile',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName'
      }
      const header = Object.keys(headers)
      const { rows } = await getUserAll({
        page: 1,
        size: this.total
      })
      const data = this.getArr(rows, headers)
      console.log(data)
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    },
    getArr(rows, headers) {
      return rows.map(itm => {
        return Object.values(headers).map(values => {
          if (values === 'timeOfEntry' || values === 'correctionTime') {
            return formatDate(itm[values])
          } else if (values === 'formOfEmployment') {
            const data = employees.hireType.filter(item => item.id === itm[values])
            return data[0] ? data[0].value : '未知'
          } else {
            return itm[values]
          }
        })
      })
    }
  }
}
</script>

